% function finding transitions in data and adding event information to
% the dataset

function EEG = findtransitions(EEG);

session = 1;
presentation = 'evoked';
lat = [];
justchanged = 0;
datcount    = 0;
prevevent   = '';
dataprob    = zeros(4,50);
for index = 1:length(EEG.event)
    EEG.event(index).session = [];    
    EEG.event(index).presentation = '';    
    if strcmpi(EEG.event(index).type, 'DI17')
        if ~isempty(lat) && EEG.event(index).latency - lat > EEG.srate*30 && datcount > 40
            fprintf('Found transition at latency %3.1f\n', (EEG.event(index).latency-1)/EEG.srate);
            if session == 1 && strcmpi(presentation, 'evoked')
                presentation = 'spontaneous';
            elseif session == 1 && strcmpi(presentation, 'spontaneous')
                session = 2;
                presentation = 'evoked';
            elseif session == 2 && strcmpi(presentation, 'evoked')
                presentation = 'spontaneous';
            elseif session == 2 && strcmpi(presentation, 'spontaneous')
                error('End of data');
            end;
            justchanged = 1;
            prevevent   = '';
            datcount    = 0;
            dataprob    = zeros(4,50);
        end;
    elseif strcmpi(EEG.event(index).type, 'DIN1')
        lat = EEG.event(index).latency;
    elseif strcmpi(EEG.event(index).type, 'DI19') || strcmpi(EEG.event(index).type, 'DI21') ...
        || strcmpi(EEG.event(index).type, 'DI25') || strcmpi(EEG.event(index).type, 'DI29')
        %EEG.event(index).type         = [ EEG.event(index).type '-' presentation ];
        EEG.event(index).session         = session;
        EEG.event(index).presentation    = presentation;
        EEG.event(index).prevevent       = prevevent;
        EEG.event(index).dataprob        = sum(dataprob,2)';
        EEG.event(index).dataprobcolumns = 'blank_light_audio_both';
        datcount = datcount + 1;
        switch EEG.event(index).type
            case 'DI19', prevevent = 'blank'; dataprob(1,datcount) = 1;
            case 'DI21', prevevent = 'light'; dataprob(2,datcount) = 1;
            case 'DI25', prevevent = 'audio'; dataprob(3,datcount) = 1;
            case 'DI29', prevevent = 'both';  dataprob(4,datcount) = 1;
        end;
        EEG.event(index).indexinsession = datcount;
    end;
end;

% statistics
n_evok  = length(strmatch('evoked', {EEG.event.presentation }));
n_spont = length(strmatch('spontaneous', {EEG.event.presentation }));
n_sess1 = length(find([EEG.event.session] == 1));
n_sess2 = length(find([EEG.event.session] == 2));
fprintf('Number of evoked events: %d\n', n_evok);
fprintf('Number of spont. events: %d\n', n_spont);
fprintf('Number of session 1 events: %d\n', n_sess1);
fprintf('Number of session 2 events: %d\n', n_sess2);

fid = fopen('results.txt', 'a');
fprintf(fid,'*************************************\n');
fprintf(fid,'Subject %s\n', EEG.setname);
fprintf(fid,'Number of evoked events: %d\n', n_evok);
fprintf(fid,'Number of spont. events: %d\n', n_spont);
fprintf(fid,'Number of session 1 events: %d\n', n_sess1);
fprintf(fid,'Number of session 2 events: %d\n', n_sess2);
fclose(fid);
